package cn.stylefeng.guns.modular.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.IndentOrder;
import cn.stylefeng.guns.modular.system.entity.PurchaseOrders;
import cn.stylefeng.guns.modular.system.mapper.PurchaseOrdersMapper;
import cn.stylefeng.guns.modular.system.model.params.PurchaseOrdersParam;
import cn.stylefeng.guns.modular.system.service.IndentOrderService;
import cn.stylefeng.guns.modular.system.service.PurchaseOrdersService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.core.util.utility.StatusCode;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.beetl.ext.simulate.BaseSimulate;
import org.quartz.SimpleTrigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;


/**
 * 订货单表控制器
 *
 * @author 飞
 * @Date 2019-07-02 21:40:17
 */
@Controller
@RequestMapping("/purchaseOrders")
public class PurchaseOrdersController extends BaseController {

    private String PREFIX = "/purchaseOrders";

    @Autowired
    private PurchaseOrdersService purchaseOrdersService;
    @Autowired
    private  PurchaseOrdersMapper purchaseOrdersMapper;
    @Autowired
    private IndentOrderService  indentOrderService;

    /**
     * 跳转到主页面
     *
     * @author 飞
     * @Date 2019-07-02
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/purchaseOrders.html";
    }

    /**
     * 新增页面
     *
     * @author 飞
     * @Date 2019-07-02
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/purchaseOrders_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 飞
     * @Date 2019-07-02
     */
    @RequestMapping("/edit")
    public String edit(@RequestParam("id") Long id,Model model) {
        model.addAttribute("id",id);
        return PREFIX + "/purchaseOrders_edit.html";
    }


    /**
     * 新增接口
     *
     * @author 飞
     * @Date 2019-07-02
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(PurchaseOrdersParam purchaseOrdersParam) {
        this.purchaseOrdersService.add(purchaseOrdersParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 飞
     * @Date 2019-07-02
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(PurchaseOrdersParam purchaseOrdersParam) {
        this.purchaseOrdersService.update(purchaseOrdersParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 飞
     * @Date 2019-07-02
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(PurchaseOrdersParam purchaseOrdersParam) {
        this.purchaseOrdersService.delete(purchaseOrdersParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 飞
     * @Date 2019-07-02
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(PurchaseOrdersParam purchaseOrdersParam) {
        PurchaseOrders detail = this.purchaseOrdersService.getById(purchaseOrdersParam.getPurchaseId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 飞
     * @Date 2019-07-02
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(PurchaseOrdersParam purchaseOrdersParam) {
        return this.purchaseOrdersService.findPageBySpec(purchaseOrdersParam);
    }

    /**
     * 获取用户当前订单的购物详情
     * @param ids
     * @return
     */
    @RequestMapping("/details")
    @ResponseBody
    public LayuiPageInfo detail(String ids,Long dealer){
        Page page = LayuiPageFactory.defaultPage();
        String[] id = ids.split(",");
        List<String> idLiST = Arrays.asList(id);
        List<Long> idLong = idLiST.stream().map(s -> Long.valueOf(s)).collect(Collectors.toList());
        IPage iPage = purchaseOrdersMapper.purchaseDetail(page,idLong,dealer);
        return  LayuiPageFactory.createPageInfo(iPage);
    }

    @RequestMapping("/submit")
    public void submit(PurchaseOrdersParam purchaseOrdersParam){
        List<PurchaseOrdersParam> purchaseOrdersParams = JSON.parseArray(purchaseOrdersParam.getPurchaseDetail(),PurchaseOrdersParam.class);

            purchaseOrdersService.submit(purchaseOrdersParams);

    }


    /**
     * 获取当前订单的所有采购单
     * @param indentOrderId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getIndentOrderByDealer/{indentOrderId}")
    public LayuiPageInfo getIndentOrderByDealer(@PathVariable  String indentOrderId){
        Page page = LayuiPageFactory.defaultPage();
        IndentOrder indentOrder = indentOrderService.getById(Long.valueOf(indentOrderId));
        List<Long> ids = Arrays.asList(indentOrder.getPurchaseId().split(",")).stream().map(s -> Long.parseLong(s)).collect(Collectors.toList());
        Page pageUse = purchaseOrdersMapper.getPurchaseByDealer(page,ids, ShiroKit.getUserNotNull().getId());
        return  LayuiPageFactory.createPageInfo(pageUse);
    }






}


